<template>
	<view>
		<view class="right-box">
			<view class="u-flex u-row-between">
				<view class="c-fff u-line-5 width70">{{name}}</view>
				<view class="gird" :style="{paddingBottom:padding}">
					<view class="u-m-t-50">
						<u-button open-type="" @click="dowload()">
							<u-icon name="download" label="下载(-2)" label-pos="bottom" style="color: deeppink;"></u-icon>
						</u-button>
						<u-button open-type="share">
							<u-icon name="share" label="分享" label-pos="bottom" style="color: #55aaff;"></u-icon>
						</u-button>
						<u-button open-type="">
							<u-icon name="heart" label="收藏" label-pos="bottom" style="color: #aa0000;"></u-icon>
						</u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'bottomRight',
		props: {
			list: {
				type: Array,
				default: function() {
					return [];
				}
			},
			image: {
				type: String,
				default: function() {
					return '';
				}
			},
			padding: {
				type: String,
				default: '100rpx'
			}
		},
		data() {
			return {}
		},
		methods: {
			async dowload() {
				const userInfo = this.$db.get("userInfo");
				let _this = this;
				if (userInfo) {
					//先扣减积分
					//扣减积分并验证用户登录等情况
					let subtractScoreResult = await this.subtractScore();
					if (!subtractScoreResult) {
						return;
					}
					uni.downloadFile({
						url: _this.image, //图片地址
						success: (res) => {
							if (res.statusCode === 200) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function() {
										uni.showModal({
											title: '下载成功',
											content: '请到图库查看！',
											showCancel: false,
											confirmText: "知道了"
										});
									}
								})
							} else {
								uni.showModal({
									title: '下载失败',
									content: res.errMsg,
									showCancel: false,
									confirmText: "知道了"
								});
							}
						}
					})

				} else {
					uni.showModal({
						title: '提示',
						content: '请先登录，再下载！',
						confirmText: "去登录",
						success: function(res) {
							if (res.confirm) {
								_this.$common.jumpToLogin()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});

				}
			},
			//扣减积分
			async subtractScore() {
				//减去积分
				const updateSubtractScore = await this.$http.get("/user/updateSubtractScore", {
					'subtractScore': 2,
					"optionType":3
				})
				const scoreRespone = await updateSubtractScore;
				console.log("scoreRespone", scoreRespone)
				if (!scoreRespone.success) {
					if (scoreRespone.errCode == 501) {
						uni.showModal({
							title: '积分不足',
							content: '账户积分不足',
							showCancel: false,
							confirmText: "去赚积分",
							success: res => {
								//跳到看广告的页面
								this.$common.switchTabTo('/pages/index/index');
							}
						});
					} else {
						this.$common.errorToShow("系统错误了，等会再试吧！！！")
					}
					return false;
				}
				return true;
			},
		}
	}
</script>

<style lang="scss">
	.right-box {
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		margin: 30rpx;
	}

	.gird {
		display: inline-grid;
	}

	.width70 {
		width: 70%;
	}
</style>